package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Menu;
import cn.wolfcode.domain.Menu;
import cn.wolfcode.mapper.MenuMapper;
import cn.wolfcode.qo.MenuQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IMenuService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MenuServiceImpl implements IMenuService {
    private MenuMapper menuMapper ;

    @Autowired
    public void setMenuMapper(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }

    @Override
    public void insert(Menu menu) {
        menuMapper.insert(menu);
    }

    @Override
    public void delete(Long id) {
        menuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Menu menu) {
        menuMapper.updateByPrimaryKey(menu);
    }

    @Override
    public Menu selectById(Long id) {
        Menu menu = menuMapper.selectByPrimaryKey(id);
        return menu ;
    }

    @Override
    public List<Menu> selectAll() {
        List<Menu> menus = menuMapper.selectAll();
        return menus ;
    }

    @Override
    public PageInfo<Menu> query(MenuQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Menu> menus = menuMapper.queryAll(qo);
        return new PageInfo<>(menus);
    }




    @Override
    public List<Menu> selectByTreeDate() {
        List<Menu> mainMenu = menuMapper.selectMainMenu(0);
        List<Menu> subMenu = new ArrayList<>();
        for (Menu menu : mainMenu) {
            Long pId = menu.getId();
            subMenu = menuMapper.selectChildren(pId);
            menu.setSubMenu(subMenu);
        }

        return mainMenu;
    }

    @Override
    public void changeState(int state , Long id) {
        List<Menu> menus = menuMapper.selectChildren(id);
        if (menus!= null){
            for (Menu menu : menus) {
                Long id1 = menu.getId();
                menuMapper.changeState(state,id1);
            }
        }
        menuMapper.changeState(state,id);
    }


}
